import { calendar } from '../../utils/calendar.js'
import {Date2} from '../../utils/date2.js'
import {getFormatToday} from '../../utils/util'

// pages/family/edit_date.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tag: '',    //外部调用标志
    curr_select: 0,   // 0 阴历 ； 1 阳历
    yangli_date: '',  //阳历时间，格式： 2020-01-01

    //阴历选择缓存
    yin_years: [],
    yin_months: [],
    yin_days: [],
    yin_year_index: 0,  //在yin_years中的下标
    yin_month_index: 0, //在yin_months中的下标
    yin_day_index: 0    //在yin_days中的下标
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      tag: options.tag || ''
    })
    //初始化阴历年份
    let years = []
    for (let i = 1900; i <= 2100; i++) {
      years.push(i)
    }
    this.setData({
      yin_years: years
    })

    //设置默认值
    this.setYinToday()
    this.setYangToday()

    //更新为传入的日期
    let date2 = new Date2()
    let ok = date2.ParseDbDateStr(options.date)
    if (ok) {
      //有传入参数
      if (date2.is_lunar) {
        //阴历
        this.setData({
          curr_select: 0
        })
        this.setYinDate(date2)

      } else {
        //阳历
        this.setData({
          curr_select: 1,
          yangli_date: options.date
        })
      }
    }
  },

  //阴历选择年份回调
  selectYinYear(year) {
    //先判断入参有效性
    let index = -1
    for (let i = 0; i < this.data.yin_years.length; i++) {
      if (this.data.yin_years[i] == year) {
        index = i
        break
      }
    }
    if (-1 == index) {
      return
    }

    this.setData({
      yin_year_index: index
    })
    //设置月份数据
    let months = []
    let leap_month = calendar.leapMonth(year)
    if (0 == leap_month) {
      //无闰月
      for (let i = 1; i <= 12; i++) {
        months.push('' + i)
      }
    } else {
      //有闰月
      for (let i = 1; i <= 12; i++) {
        months.push('' + i)
        if (i == leap_month) {
          months.push('闰' + i)
        }
      }
    }

    this.setData({
      yin_months: months
    })
    if (this.data.yin_month_index >= months.length) {
      this.selectYinMonth(months[months.length - 1])
    } else {
      this.selectYinMonth(months[this.data.yin_month_index])
    }
  },

  //阴历选择月份回调
  selectYinMonth(monthStr) {
    //先判断参数的有效性
    let index = -1
    for (let i = 0; i < this.data.yin_months.length; i++) {
      if (this.data.yin_months[i] == monthStr) {
        index = i
        break
      }
    }
    if (-1 == index) {
      return
    }

    this.setData({
      yin_month_index: index
    })
    let day_num = 0;  //本月天数
    let curr_yin_year = this.data.yin_years[this.data.yin_year_index]
    if (curr_yin_year === undefined) {
      return
    }
    if (monthStr[0] == "闰") {
      //闰月
      let m = parseInt(monthStr.substr(1))
      if (isNaN(m)) {
        return
      }
      day_num = calendar.leapDays(curr_yin_year)
    } else {
      let m = parseInt(monthStr)
      if (isNaN(m)) {
        return
      }
      day_num = calendar.monthDays(curr_yin_year, m)
    }
    let days = []
    for (let i = 1; i <= day_num; i++) {
      days.push(i)
    }
    this.setData({
      yin_days: days
    })
    if (this.data.yin_day_index >= days.length) {
      this.setData({
        yin_day_index: days.length - 1
      })
    }
  },

  //设置阴历为某天, 参数是Date2对象
  setYinDate(date2) {
    if (!date2.is_lunar) {
      return
    }
    this.selectYinYear(date2.year)
    let monthStr = date2.is_leap_month ? '闰' + date2.month : '' + date2.month
    this.selectYinMonth(monthStr)
    this.setData({
      yin_day_index: date2.day - 1
    })
  },

  //设置阴历选择器为阴历的今天
  setYinToday() {
    let today = Date2.GetLunarToday()
    this.setYinDate(today)
  },

  //设置阳历选择器为阳历的今天
  setYangToday() {
    this.setData({
      yangli_date: getFormatToday()
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  bindLiChange(e) {
    let cs = parseInt(e.detail.value)
    if (cs == 0 || cs == 1) {
      this.setData({
        curr_select: cs
      })
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  bindYinLiChange(e) {
    let indexArr = e.detail.value
    if (indexArr[0] != this.data.yin_year_index) {
      this.selectYinYear(this.data.yin_years[indexArr[0]])
    } else if (indexArr[1] != this.data.yin_month_index) {
      this.selectYinMonth(this.data.yin_months[indexArr[1]])
    } else if (indexArr[2] != this.data.yin_day_index) {
      this.setData({
        yin_day_index: this.data.yin_days[indexArr[2]] - 1
      })
    }
  },

  bindYangLiChange: function(e) {
    this.setData({
      yangli_date: e.detail.value
    })
  },

  bindCancel(e) {
    wx.navigateBack()
  },

  //获取选择器当前的Date2对象, 异常情况返回null
  getCurrDate2() {
    if (this.data.curr_select == 0) {
      //阴历
      let year = this.data.yin_years[this.data.yin_year_index]
      if (year === undefined) {
        return null
      }
      let month = this.data.yin_months[this.data.yin_month_index]
      if (month === undefined) {
        return null
      }
      let is_leap_month = false
      if (month[0] == '闰') {
        is_leap_month = true
        month = month.substr(1)
      }
      let day = this.data.yin_days[this.data.yin_day_index]
      if (day === undefined) {
        return null
      }

      return new Date2(true, year, month, day, is_leap_month)

    } else {
      //阳历
      if (this.data.yangli_date.length == 0) {
        return null
      }
      let date2 = new Date2()
      date2.ParseDbDateStr(this.data.yangli_date)
      return date2
    }
  },

  bindConfirm(e) {
    let pages = getCurrentPages()  //获取当前页面js里面的pages里的所有信息。
    let prevPage = pages[pages.length - 2]  //prevPage 是获取上一个页面的js里面的pages的所有信息。 -2 是上一个页面，-3是上上个页面以此类推。

    let date2 = this.getCurrDate2()
    if (date2) {
      if (prevPage.editDateCallback) {
        prevPage.editDateCallback(date2, this.data.tag)
      }
    }
    wx.navigateBack()
  }
})